package top.waterduck.business.controller.web;

import top.waterduck.server.dto.TestDto;
import top.waterduck.server.dto.PageDto;
import top.waterduck.server.dto.ResponseDto;
import top.waterduck.server.service.TestService;
import top.waterduck.server.utils.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @author: duck
* @date: 2020-09-17 18:44
* @desc:控制类
**/
@Slf4j
@RestController
@RequestMapping("/web/test")
public class TestController {
    public static final String BUSINESS_NAME="";

    @Resource
    private TestService testService;

    /**
    * 获取列表
    * @param page
    * @param size
    * @return
    */
    @GetMapping("/list")
    public ResponseDto list(@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "size",defaultValue = "10") Integer size){
        ResponseDto responseDto = new ResponseDto();
        PageDto pageDto=new PageDto();
        pageDto.setPage(page);
        pageDto.setSize(size);
        testService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
    * 保存信息
    * @param testDto
    * @return
    */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody TestDto testDto) {
        //保存校验
        ValidatorUtil.length(testDto.getName(),"",1,255);
        ResponseDto responseDto = new ResponseDto();
        testService.save(testDto);
        responseDto.setContent(testDto);
        return responseDto;
    }

    /**
    * 根据主键删除信息
    * @param id
    * @return
    */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        testService.delete(id);
        return responseDto;
    }
}